<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\cpmjagent\controller\user;

/**
 * Description of GameUserController
 *
 * @author SoChishun
 */
class GameuserController extends \app\cpmjagent\controller\AppbaseController {

    public function referrallist($word = '') {
        \think\Db::name('')->execute('update t_users set recharge_amount_total=(select IFNULL(sum(amount),0) from t_finance_recharge where user_id=t_users.userid and status=1)');
        $account = $this->global_data['account'];
        if ($this->global_data['isadmin']) {
            $where = [];
            $tpl = 'referrallist_admin';
        } else {
            $where = ['referral_qd' => $account];
            $tpl = 'referrallist_admin';
        }
        if ($word) {
            $where = ['account|userid' => ['like', $word]];
        }
        $search['word'] = $word;
        $list = \app\cpmjagent\model\user\GameUserModel::where($where)->order('userid desc')->paginate(20);
        $firstRowId = ($list->getCurrentPage() - 1) * $list->listRows();
        return view($tpl, ['list' => $list, 'firstRowId' => $firstRowId, 'search' => $search]);
    }
    public function get_username($userid=0){
        if(!$userid){
            return '用户不存在!';
        }
        $value=\app\cpmjagent\model\user\GameUserModel::where('userid',$userid)->value('name');
        return $value?base64_decode($value):$value;
    }

    public function buygems($id = 0) {
        if (!$id) {
            exit('参数无效!');
        }
        $myuid = $this->global_data['userid'];
        $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $myuid])->value('gems');
        if (!$mygems) {
            exit('您的房卡余额不足，请先充值!');
        }
        return view('', ['userid' => $id, 'mygems' => $mygems]);
    }

    public function doBuyGemsSave($userid = 0, $number = 0) {
        if (!$userid || !$number || !is_numeric($number) || $number < 1) {
            $this->error('参数无效');
        }
        $where = ['userid' => $userid];
        $mGameUser = new \app\cpmjagent\model\user\GameUserModel();
        $gameuser_account = $mGameUser->where($where)->value('account');
        if (!$gameuser_account) {
            $this->error('充值用户不存在');
        }
        $admin_account = $this->global_data['account'];
        $admin_uid = $this->global_data['userid'];
        $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->value('gems');
        if ($mygems < $number) {
            $this->error('充值数量不能超过您的房卡余额!');
        }
        $mGameUser->where($where)->setInc('gems', $number); // 玩家增加房卡
        \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->setDec('gems', $number); // 渠道减少房卡
        $out_trade_no = 'HTDLZCWJ' . date('YmdHis') . $admin_uid;
        // 玩家房卡交易记录
        $data = ['account' => $gameuser_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'agent_direct_recharge_gameuser', 'gems' => $number, 'user_id' => $userid, 'remark' => "[$admin_account]给玩家[$gameuser_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        // 渠道房卡交易记录
        $data = ['account' => $admin_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'agent_direct_recharge_gameuser', 'gems' => 0 - $number, 'user_id' => $admin_uid, 'remark' => "[$admin_account]给玩家[$gameuser_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        $this->success('充值成功!', '!window.parent.location.reload();', '', 1);
    }

    public function rechargegems() {
        $myuid = $this->global_data['userid'];
        $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $myuid])->value('gems');
        if (!$mygems) {
            exit('您的房卡余额不足，请先充值!');
        }
        return view('', ['mygems' => $mygems]);
    }

    public function doRechargeGemsSave($userid = 0, $number = 0) {
        if (!$userid || !$number || !is_numeric($number) || $number < 1) {
            $this->error('参数无效');
        }
        $where = ['userid' => $userid];
        $mGameUser = new \app\cpmjagent\model\user\GameUserModel();
        $gameuser_account = $mGameUser->where($where)->value('account');
        if (!$gameuser_account) {
            $this->error('充值用户不存在');
        }
        $admin_account = $this->global_data['account'];
        $admin_uid = $this->global_data['userid'];
        $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->value('gems');
        if ($mygems < $number) {
            $this->error('充值数量不能超过您的房卡余额!');
        }
        $mGameUser->where($where)->setInc('gems', $number); // 玩家增加房卡
        \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->setDec('gems', $number); // 渠道减少房卡
        $out_trade_no = 'HTDLZCWJ' . date('YmdHis') . $admin_uid;
        // 玩家房卡交易记录
        $data = ['account' => $gameuser_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'agent_direct_recharge_gameuser', 'gems' => $number, 'user_id' => $userid, 'remark' => "[$admin_account]给玩家[$gameuser_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        // 渠道房卡交易记录
        $data = ['account' => $admin_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'agent_direct_recharge_gameuser', 'gems' => 0 - $number, 'user_id' => $admin_uid, 'remark' => "[$admin_account]给玩家[$gameuser_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        $this->success('充值成功!',url('rechargegems'));
    }

    public function doGameUserDelete($id = '') {
        if (!$id) {
            return ['status' => false, 'info' => '参数无效'];
        }
        \app\cpmjagent\model\user\AgentModel::destroy($id);
        return ['status' => true];
    }

}
